<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Principal extends CI_Controller {

	function __construct()
	{
		parent::__construct();
			
		$this->load->database();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->model('TipoPropiedad','',TRUE);
		$this->load->model('Propiedad','',TRUE);
		$this->load->model('Emprendimiento','',TRUE);
		$this->load->model('ContactoModel','',TRUE);
		$this->load->model('Zona','',TRUE);
		require_once 'application/libraries/swift_mailer/swift_required.php';
		
	}
	
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		//$this->load->view('view_contacto');
		$propiedades = $this->Propiedad->get_by_portada()->result();
		$tipospropiedad = $this->TipoPropiedad->get_paged_list(30, 0)->result();
		$zonas = $this->Zona->get_paged_list(30, 0)->result();
				
		// load view
		$data['tipospropiedad'] = $tipospropiedad;
		$data['zonas'] = $zonas;
		$data['propiedades'] = $propiedades;
		$data['esHome'] = 1;
		$data['barraNav'] = 0;
		$out = $this->load->view('view_home', $data, TRUE);
		$data['out'] = $out;
		$this->load->view('view_template', $data);
	}
	
		public function test()
	{
		//$this->load->view('view_contacto');
		/*$propiedades = $this->Propiedad->get_by_portada()->result();
		$tipospropiedad = $this->TipoPropiedad->get_paged_list(30, 0)->result();
		$zonas = $this->Zona->get_paged_list(30, 0)->result();
				
		// load view
		$data['tipospropiedad'] = $tipospropiedad;
		$data['zonas'] = $zonas;
		$data['propiedades'] = $propiedades;*/
		$data['esHome'] = 1;
		/*
		$data['barraNav'] = 0;
		$out = $this->load->view('view_home', $data, TRUE);
		$data['out'] = $out;*/
		$this->load->view('view_test', $data);
	}
	
	
	public function contacto($confirmacion=null)
	{
		//$this->load->view('view_contacto');
		$propiedades = $this->Propiedad->get_by_portada()->result();
		$tipospropiedad = $this->TipoPropiedad->get_paged_list(30, 0)->result();
		$zonas = $this->Zona->get_paged_list(30, 0)->result();
				
		// load view
		$data['tipospropiedad'] = $tipospropiedad;
		$data['zonas'] = $zonas;
		$data['confirmacion'] = $confirmacion;
		$data['propiedades'] = $propiedades;
		$data['barraNav'] = 0;
		$data['esHome'] = 0;
		$out = $this->load->view('view_contacto', $data, TRUE);
		$data['out'] = $out;
		$this->load->view('view_template', $data);
	}
	
	public function addContacto($idMotivo)
	{

		if ($idMotivo==2 || $idMotivo==1){
			$data = array(
				   'idMotivoContacto' => $idMotivo,
				   'zona' => $this->input->post('zona'),
				   'presupuesto' => $this->input->post('presupuesto'),
				   'cantDormitorios' => $this->input->post('cantDormitorio'),
				   'nombre' => $this->input->post('nombre'),
				   'email' => $this->input->post('email'),
				   'telefono' => $this->input->post('telefono'),
				   'comentario' => $this->input->post('comentario'),
				);
		}else if ($idMotivo==3 || $idMotivo==5){
			$data = array(
				   'idMotivoContacto' => $idMotivo,
				   'direccionTasacion' => $this->input->post('direccionTasacion'),
				   'nombre' => $this->input->post('nombre'),
				   'email' => $this->input->post('email'),
				   'telefono' => $this->input->post('telefono'),
				   'comentario' => $this->input->post('comentario'),
				);
		}else if ($idMotivo==4 || $idMotivo==6){
			$data = array(
				   'idMotivoContacto' => $idMotivo,
				   'nombre' => $this->input->post('nombre'),
				   'email' => $this->input->post('email'),
				   'telefono' => $this->input->post('telefono'),
				   'comentario' => $this->input->post('comentario'),
				);
		}
		//var_dump($data);
		//die();
		
		$id = $this->ContactoModel->insert($data);
		
		if ($idMotivo==6)
			$destinos = array('arothar@gmail.com'  => $this->input->post('email'),'marta@melamudpropiedades.com'  => $this->input->post('email'), 'alquileres@melamudpropiedades.com' => $this->input->post('email'));
		else
			$destinos = array('sol@melamudpropiedades.com'  => $this->input->post('email'));
		
		$comentario = $this->input->post('comentario');
		if ($this->input->post('propiedadConsultada') != null && $this->input->post('propiedadConsultada') != ""){
			$comentario = $comentario . "<br> Se consulto por la propiead:" .$this->input->post('propiedadConsultada');
		}
		
		$this->enviarMail($destinos, $this->input->post('email'), $comentario);

		echo 'Se registro correctamente su consulta';
		//redirect('principal/contacto/1');
	}
	
	public function enviarPropiedad($id)
	{
		$destinos = array($this->input->post('suMail')  => $this->input->post('suMail'));
		//$mensaje = "";
		$mensaje = $this->load->view('email-template/templateComparte.php', '', TRUE);
		$this->enviarMail($destinos, $this->input->post('tuMail'), $mensaje);
		//redirect('propiedades/ficha/' . $id);
		echo 'Se envio correctamente la propiedad.';
	}
	
	public function empresa()
	{
		$tipospropiedad = $this->TipoPropiedad->get_paged_list(30, 0)->result();
		$zonas = $this->Zona->get_paged_list(30, 0)->result();
				
		// load view
		$data['tipospropiedad'] = $tipospropiedad;
		$data['zonas'] = $zonas;
		$data['barraNav'] = 0;
		$data['esHome'] = 0;
		// load view
		$data['tipospropiedad'] = $tipospropiedad;
		$this->mostrarVista('view_empresa',$data);
	}
	
	public function servicios()
	{
		$tipospropiedad = $this->TipoPropiedad->get_paged_list(30, 0)->result();
		$zonas = $this->Zona->get_paged_list(30, 0)->result();
				
		// load view
		$data['tipospropiedad'] = $tipospropiedad;
		$data['zonas'] = $zonas;
		$data['barraNav'] = 0;
		$data['esHome'] = 0;
		$this->mostrarVista('view_servicios',$data);
	}
	
	public function emprendimientos()
	{
		$emprendimientos = $this->Emprendimiento->get_paged_list(30, 0)->result();
		$zonas = $this->Zona->get_paged_list(30, 0)->result();
		
		$data['emprendimientos'] = $emprendimientos;
		$data['zonas'] = $zonas;
		$data['barraNav'] = 0;
		$data['esHome'] = 0;
		$out = $this->load->view('view_emprendimientos', $data, TRUE);
		$data['out'] = $out;
		$this->load->view('view_template', $data);
	}
	
	public function newsletter()
	{
		$emprendimientos = $this->Emprendimiento->get_paged_list(30, 0)->result();
		$zonas = $this->Zona->get_paged_list(30, 0)->result();
		
		$data['emprendimientos'] = $emprendimientos;
		$data['zonas'] = $zonas;
		$data['barraNav'] = 0;
		$data['esHome'] = 0;
		$out = $this->load->view('view_newsletter', $data, TRUE);
		$data['out'] = $out;
		$this->load->view('view_template', $data);
	}
	
	public function mostrarVista($vista, $data)
	{
		$out = $this->load->view($vista, null, TRUE);
		$data['out'] = $out;
		$this->load->view('view_template', $data);
	}
	
	function enviarMail($arrayDestinos, $emailEmisor,$contenido){
		$smtp = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
		  ->setUsername('sol@melamudpropiedades.com')
		  ->setPassword('losmelamud');

		$mailer = Swift_Mailer::newInstance($smtp);

		$message = Swift_Message::newInstance('Consulta via Web');
		
		
		$message
		  ->setTo($arrayDestinos)
		  //->setCc(array($emailCC))
		  ->setFrom(array($emailEmisor => $emailEmisor))
		  ->setBody($contenido,'text/html')
		  ;

		$mailer->send($message);
		/*
		if ($mailer->send($message))
		{
		  echo "Message sent!";
		}
		else
		{
		  echo "Message could not be sent.";
		}
		die();
		*/
	}
	
	function getFotosHead($esHome){
		$this->load->helper('directory');

		if ($esHome == 1){
			$head = 'headHome';
			$map = directory_map(getcwd().'/assets/melamud/img/headHome/');
		}else {
			$head = 'head';
			$map = directory_map(getcwd().'/assets/melamud/img/head/');
		}
		//$result[] = null;
		foreach ($map as $k => $v)
		{
			if (!is_array($v)) {
				  $result[] = array(
					'name' => $v,
					'path' => base_url() . 'assets/melamud/img/'. $head .'/' . $v,
				  );
			}
		}
		//var_dump($result);
		echo json_encode($result); 
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */